package com.leedsoft.controller.vo;

import com.google.gson.reflect.TypeToken;
import com.leedsoft.common.JSON;
import com.leedsoft.domain.AidTask;

import java.util.Arrays;
import java.util.List;

/**
 * Created by leeds on 2017/6/20.
 */
public class TaskJsonVo extends AidTask{
    private Integer taskType = 0;//0 新建、1指向已存在
    //当前是分支环节时
    private String exp;
    private String label;//箭头说明
    private String brief;//任务简介
    //当操作类型是通知时
    private List<Long> notifiedIds;
    private String notifyTpl;

    public void parseOperationCode() {
        if(getOperation_code() != null) {
            switch (getOperation_code()) {
                case 0://通知:{notifiedIds:"1,2,3", notifyTpl:"请添加通知模版。如：紧急通知！"}
                    NoticeJsonVo notice = JSON.gson.fromJson(getOperation_info(), NoticeJsonVo.class);
                    notifiedIds = Arrays.asList(notice.getNotifiedIds());
                    notifyTpl = notice.getNotifyTpl();
                    break;
            }
        }
    }

    @Override
    public String getOperation_info() {
        if(super.getOperation_info() == null && notifiedIds != null) {
            NoticeJsonVo noticeJsonVo = new NoticeJsonVo();
            noticeJsonVo.setNotifiedIds(notifiedIds.toArray(new Long[notifiedIds.size()]));
            noticeJsonVo.setNotifyTpl(notifyTpl);
            setOperation_info(JSON.gson.toJson(noticeJsonVo));
        }
        return super.getOperation_info();
    }

    public static void main(String[] args) {
        String taskJson = "[{\"assignee\":\"14\",\"detail\":\"zhang\",\"switch_logic\":\"wang\",\"operation_code\":\"0\",\"notifiedIds\":[\"2\",\"5\",\"14\"],\"notifyTpl\":\"leeds\"},{\"assignee\":\"14\",\"detail\":\"zhang\",\"switch_logic\":\"wang\",\"operation_code\":\"0\",\"notifiedIds\":[\"2\",\"5\",\"14\"],\"notifyTpl\":\"leeds\"}]";
//        List<TaskJsonVo> taskJsonVoList = JSON.parseArray(taskJson, TaskJsonVo.class);
        List<TaskJsonVo> taskJsonVoList = JSON.gson.fromJson(taskJson, new TypeToken<List<TaskJsonVo>>(){}.getType());
//        List<TaskJsonVo> taskJsonVoList = JSON.fromJson(taskJson);
        for (TaskJsonVo task : taskJsonVoList) {
            task.setHas_to((short) 0);
        }
    }


    public Integer getTaskType() {
        return taskType;
    }

    public void setTaskType(Integer taskType) {
        this.taskType = taskType;
    }

    public String getExp() {
        return exp;
    }

    public void setExp(String exp) {
        this.exp = exp;
    }

    public String getBrief() {
        brief = getDetail();
        return brief;
    }

    public void setBrief(String brief) {
        this.brief = brief;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
}
